<?php
namespace app\index\controller;
use think\Controller;
class Common extends Controller
{

    public function _initialize(){
    	//当前位置
       /*  if(!session('id') || !session('mobile')){
           $this->error('您尚未登录系统',url('member/login/index')); 
        }*/
        if(input('cateid')){
            $this->getPos(input('cateid'));
        }
        if(input('artid')){
            $articles=db('article')->field('cateid')->find(input('artid'));
            $cateid=$articles['cateid'];
            $this->getPos($cateid);
        }
        //网站配置项
    	$this->getConf();
        //网站分类导航
        $this->getNavCates();
        $this->getProCates();
        $this->getHots();
        $this->getHotproducts();
        //底部导航信息
        $cateM=new \app\index\model\Cate();
        $recBottom=$cateM->getRecBottom();
        $this->assign('recBottom',$recBottom);
        $request = \think\Request::instance();
        $this->assign("controllname",$request->controller());

        /*广告列表开始*/
        $adones=$this->getAds(1);//导航条下面的24个广告位
        $this->assign("adones",$adones);
        
         $adfours=$this->getAds(4);//首页顶部一个广告位
        $this->assign("adfours",$adfours);
         $adfives=$this->getAds(5);//导航条下面的24个广告位
        $this->assign("adfives",$adfives);
         $adsixs=$this->getAds(6);//导航条下面的24个广告位
        $this->assign("adsixs",$adsixs);
        /*广告列表结束*/
        /*首页导航*/
        $navs=db('nav')->order("sort DESC")->select();
        $this->assign('navs',$navs);
        /*首页导航*/

        /*首页导航*/
        $keywords=db('keyword')->order("click DESC limit 5")->select();
        $this->assign('keywords',$keywords);
        /*首页导航*/
        $navid=input('navid');
        //echo $navid;
        $this->assign("navid",$navid);
        

    }

    public function getAds($adcate){
        $ads=db('ad')->where(array('cateid'=>$adcate))->select();
        return $ads;
    }
    
    public function getHots(){
        $hots=db('article')->order('click DESC limit 10')->select();
        
        $this->assign('hots',$hots);
    }

     public function getHotproducts(){
        $hotproducts=db('product')->order('click DESC limit 10')->select();
        
        $this->assign('hotproducts',$hotproducts);
    }


    public function getProCates(){
       $procates=db('procate')->where(array('pid'=>0))->order('sort DESC')->select();
        foreach ($procates as $k => $v) {
            $children=db('procate')->where(array('pid'=>$v['id']))->order('sort DESC')->select();
            if($children){
                $procates[$k]['children']=$children;
            }else{
                $procates[$k]['children']=0;
            }
        }
        //print_r($cateres);
        $this->assign('procates',$procates); 
    }

    public function getNavCates(){
        $cateres=db('procate')->where(array('pid'=>0))->order('sort DESC')->select();
        foreach ($cateres as $k => $v) {
            $children=db('procate')->where(array('pid'=>$v['id']))->select();
            if($children){
                $cateres[$k]['children']=$children;
            }else{
                $cateres[$k]['children']=0;
            }
        }
        //print_r($cateres);
        $this->assign('cateres',$cateres);
    }

    public function getConf(){
        $conf=new \app\index\model\Conf();
        $_confres=$conf->getAllConf();
        $confres=array();

        foreach ($_confres as $k => $v) {
            $confres[$v['enname']]=$v['value'];
        }
       // print_r($confres);
        $this->assign('confres',$confres);
    }

    public function getPos($cateid){
        $cate= new \app\index\model\Cate();
        $posArr=$cate->getparents($cateid);
       
        $this->assign('posArr',$posArr);
    }


}
